package models;

import util.common.ConvertUtil;
import util.common.UStringUtil;

import javax.persistence.*;
import java.util.List;

/**
 * Created by liming on 16/7/12.
 */
@Entity
@Table(name = "admin_supplier_contacts")
public class AdminSupplierContacts extends BaseModel {

    /**
     * 供应商
     */
    @ManyToOne
    @JoinColumn(name = "admin_supplier_id")
    public AdminSupplier adminSupplier;

    /**
     * 联系人名称
     */
    @Column(name = "name")
    public String name;

    /**
     * 联系电话
     */
    @Column(name = "phone")
    public String phone;

    /**
     * 备注
     */
    @Column(name = "remark")
    public String remark;

    /**
     * 查询供应商联系人
     * @param supplierId
     * @return
     */
    public static List<AdminSupplierContacts> findBySupplierId(long supplierId){
        return AdminSupplierContacts.find("adminSupplier.id = ?",supplierId).fetch();
    }

    /**
     * 删除联系人
     */
    public static void deleteByIds(Integer[] delIds){
        String delIdStr = UStringUtil.concatStr(",",delIds);
        AdminSupplierContacts.delete("id in (" + ConvertUtil.escapeSql(delIdStr) + ")");
    }

    /**
     * 删除供应商下的所有联系人
     */
    public static void deleteBySupplierId(long id){
        AdminSupplierContacts.delete("adminSupplier.id = ?",id);
    }
}
